package com.zdj.data.net;

import android.os.Handler;

import com.example.udpsocket.udpSocketApi;
import com.lkskt.frame.jni.Liblksjni;
import com.zdj.utils.MyLogUtil;
import com.zdj.utils.StringUtils;

/**
 * Created by dengjie zhang on 2016/3/31.
 */
public class UDPProxy {

    private static final String TAG = "UDPProxy";
    /**
     * 未建立连接状态
     */
    public static final int IS_UNCONNECT = 0;
    /**
     * 建立连接却未登录状态
     */
    public static final int IS_CONNECT = 1;
    /**
     * 建立连接且登录中
     */
    public static final int IS_LOGIN = 2;


   /* static {
        System.loadLibrary("udpsocket");
//        System.loadLibrary("lksjni");
    }*/

    private static final int MSG = 1;


    public static udpSocketApi udp;

    public static void initUDP(Handler handler,String ip) {
        initSocket(handler,ip);
    }

    public static void init(String host, int port, String uuid) {
        udp.init(host, port, uuid);
    }

    public static boolean connect() {
        return udp.connect();
    }

    public static int login(String username, String pwd,String regId) {
        return Liblksjni.getInstance().lksudprequest(Liblksjni.SS_LOGIN, new StringBuffer().append(username)
                .append(",").append(pwd).append(",").append(regId).append(",").append(StringUtils.randomStr(15)).append(",").toString());
    }

    public static int login(String username, String pwd,String regId,int userType) {
        return Liblksjni.getInstance().lksudprequest(Liblksjni.SS_LOGIN, new StringBuffer().append(username)
                .append(",").append(pwd).append(",").append(regId).append(",")
                .append(StringUtils.randomStr(15)).append(",")
                .append(userType).append(",").toString());
    }


    public static void getOfflineMsg(int msgId) {
        Liblksjni.getInstance().lksudprequest(Liblksjni.SS_GETOFFMSG, msgId + ",");
    }


    public static void logout() {
        Liblksjni.getInstance().lksudprequest(Liblksjni.SS_EXIT, "");
    }


    public static boolean reconnect() {
        return udp.reconnect();
    }


    public static boolean isconnect() {
        return udp.isconnect();
    }


    public static boolean close() {
        logout();
        return udp.close();
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        udp.close();
    }

    public static boolean setCallBack(Object obj) {
        return udp.setCallBack(obj);
    }

    /**
     * <br/>
     * 作者 ：dengjie zhang <br/>
     * created at 2016/4/6 10:44
     *
     * @return IS_UNCONNECT = 0,//未建立连接状态
     * IS_CONNECT=1,//建立连接却未登录状态
     * IS_LOGIN=2//建立连接且登录中
     * -1   udp为空
     */
    public static int getState() {
        if (udp != null) {
            return udp.getState();
        }
        return -1;
    }


    private static void initSocket(Handler handler, String ip) {
        Liblksjni.getInstance().setHandler(handler);
        int code = Liblksjni.getInstance().lksudprequest(Liblksjni.SS_ANDROID_INIT, ip + ",7777,");
        MyLogUtil.d(TAG, "initSocket: \n" + code);
    }
}
